import axios from 'axios'
import {
  znk
} from '@/util/public'
import store from '@/vuex/mutations'
let base = 'http://139.199.75.210:8082'
// 创建子类
let instance = axios.create({})
// 二次登陆验证
// let oldinst = axios.create({})
// oldinst.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'
// oldinst.defaults.headers.common['Authorization'] = 'Basic cGlnOnBpZw=='
// 设置请求头
instance.defaults.headers.post['Content-Type'] = 'application/json;'
instance.defaults.headers.common['Authorization'] = 'Basic cGlnOnBpZw=='
// 添加响应拦截器
instance.interceptors.request.use(config => {
  let token = znk.getCookie('token')
  let userId = znk.getCookie('userId') || ''
  // 判断是否存在token，如果存在的话，则每个http header都加上token
  if (token) {
    config.headers.Authorization = token
    config.headers.token = token
    config.headers.userId = userId
  }
  if (config.token) { // 临时验证
    config.headers.Authorization = config.token
  }
  // if (!token) {
  //   window.location.href = '/login'
  // }
  return config
}, error => {
  return Promise.reject(error)
})
// http response 拦截器
instance.interceptors.response.use(
  response => {
    if (response.status === 200) {
      if (response.data.code !== 501) {
        if (response.data.code !== 0) {
          if (store && store.state && store.state.that && store.state.that.$message) {
            store.state.that.$message({
              message: response.data.msg || '请求错误！',
              type: 'error'
            })
            store.state.that.$store.commit('LOADING', false)
          }
          return Promise.resolve(response)
        }
        return Promise.resolve(response)
      } else {
        window.location.href = '/login'
      }
    } else {
      // 这个地方可以由后台编辑状态码区分不同情况，做不同的逻辑处理
      return Promise.reject(response)
    }
    // return response
  },
  error => {
    if (error.response) {
      switch (error.response.status) {
        case 501:
          // 返回 401 清除token信息并跳转到登录页面
          znk.getCookie('token')
          // let alone = sessionStorage.getItem("alone")
          window.location.href = '/login'
          break
        case 506:
          // Message.error("当前操作没有权限");
          console.warn('当前操作没有权限')
          break
      }
    }
    return Promise.reject(error.message) // 返回接口返回的错误信息
  })
// 对外接口
export function Http (method, url, params) {
  try {
    store.commit('LOADING', true)
    // let token = znk.getCookie('token')
    // if (!token) {
    //   window.location.href = '/login'
    // }
    if (method === 'get' || method === 'GET') {
      return get(base + url, params)
    } else if (method === 'post' || method === 'POST') {
      return post(base + url, params)
    } else if (method === 'put' || method === 'PUT') {
      return put(base + url, params)
    } else if (method === 'del' || method === 'DEL') {
      return del(base + url, params)
    }
  } catch (err) {
    store.commit('LOADING', true)
  }
}

// 封装get方法
// eslint-disable-next-line no-unused-vars
function get (url, params) {
  return new Promise((resolve, reject) => {
    instance.get(url, {
      params: params
    }).then(res => {
      resolve(res.data)
    }).catch(err => {
      store.commit('LOADING', true)
      if (err && err.data) {
        err.data.data.total = ''
      }
      reject(err.data)
    })
  })
}
// 封装post方法
// eslint-disable-next-line no-unused-vars
function post (url, params) {
  return new Promise((resolve, reject) => {
    instance.post(url, params).then(res => {
      // store.commit('LOADING', false)
      resolve(res.data)
    }).catch(err => {
      store.commit('LOADING', true)
      reject(err.data)
    })
  })
}
// 封装put方法
// eslint-disable-next-line no-unused-vars
function put (url, params) {
  return new Promise((resolve, reject) => {
    instance.put(url, params).then(res => {
      // store.commit('LOADING', false)
      resolve(res.data)
    }).catch(err => {
      store.commit('LOADING', true)
      reject(err.data)
    })
  })
}
// 封装put方法
// eslint-disable-next-line no-unused-vars
function del (url, params) {
  return new Promise((resolve, reject) => {
    instance.delete(url, params).then(res => {
      // store.commit('LOADING', false)
      resolve(res.data)
    }).catch(err => {
      store.commit('LOADING', true)
      reject(err.data)
    })
  })
}
// 获取登录用户
// export const login = params => {
//   return instance.post(`${base}/admin/auth/login`, params).then(res => res.data)
// }
